/**
 * Created by LUOLIANG on 2016/6/12.
 */
var billnotes = avalon.define({
    $id: "billnoteController",
    income: "",
    pay: "",
    user: "",

    userInfo: function () {
        $.ajax({
            type: "post",
            url: "/CounterDrops/weuser/currentUser",
            data: {
                openid: $.cookie("openid")
            },
            success: function (data) {
                if (data.success == true) {
                    billnotes.user = data.model;
                } else {
                    toastr.warning(data.msg);
                }
            }
        });
    },

    outlogin: function () {
        $.ajax({
            type: "post",
            url: "/CounterDrops/weuser/outLogin",
            data: {},
            success: function (data) {
                if (data.success == true) {
                    window.location.href = "/CounterDrops/index/wechat/login";
                } else {
                    toastr.warning(data.msg);
                }
            }
        });
    },

    save: function (type) {
        if (type == 'income' && $("#instruction").val() != '' && billnotes.income != '') {
            billnotes.excutesave("收入", $("#instruction").val(), billnotes.income, jQuery("#select1 option:selected").text());
        } else if (type == 'pay' && $("#instruction1").val() != '' && billnotes.pay != '') {
            billnotes.excutesave("支出", $("#instruction1").val(), billnotes.pay, jQuery("#select2 option:selected").text());
        } else {
            toastr.warning("请填写完整信息...");
        }
    },

    excutesave: function (type, orderinstruction, charges, select) {
        $.ajax({
            type: "post",
            url: "/CounterDrops/weOrder/initorder",
            data: {
                incometype: type,
                ordertype: select,
                orderinstruction: orderinstruction,
                charges: charges,
            },
            success: function (data) {
                toastr.info(data.msg);
                setTimeout(function () {
                    window.location.reload();
                }, 2000);
            },
        });
    }

});
billnotes.userInfo();
